package com.shop2cn.iapi.sqdistributor.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqdistributor.parameter.request.GetAgencyDistributorConfigReq;
import com.shop2cn.iapi.sqdistributor.parameter.request.pojo.AgencySubDistributorCommisionRateVo;
import com.shop2cn.iapi.sqdistributor.parameter.response.GetAgencyDistributorConfigResponse;
import com.shop2cn.iapi.sqdistributor.service.GetAgencyDistributorConfigCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.sqdistributor.DistributionAgencySubDistributorCommisionRate;
import com.ymttest.database.model.sqdistributor.DistributionAgencySubDistributorConfig;
import com.ymttest.database.sqlwapper.sqdistributor.DistributionAgencySubDistributorCommisionRateWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.DistributionAgencySubDistributorConfigWrapper;
import com.ymttest.utils.DataUtil;

public class Ts_GetAgencyDistributorConfig {
	
	//java接口
	private static GetAgencyDistributorConfigReq getAgencyDistributorConfigReq;
	private static GetAgencyDistributorConfigCall getAgencyDistributorConfigCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("分销机构查询分销员配置");
	}

	@Before
	public void caseUp() {
		getAgencyDistributorConfigReq = new GetAgencyDistributorConfigReq();
		getAgencyDistributorConfigCall = new GetAgencyDistributorConfigCall();	
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getAgencyDistributorConfigCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb() throws Exception {

		DistributionAgencySubDistributorConfigWrapper distributionAgencySubDistributorConfigWrapper = new DistributionAgencySubDistributorConfigWrapper();
		
		DistributionAgencySubDistributorConfig config = distributionAgencySubDistributorConfigWrapper
				.selectByMerchantIdAndDistributorId(
						getAgencyDistributorConfigReq.getMerchantId(),
						getAgencyDistributorConfigReq.getDistributorId());
		
		GetAgencyDistributorConfigResponse response = getAgencyDistributorConfigCall
				.getAgencyDistributorConfigResponse();

		List<AgencySubDistributorCommisionRateVo> expectResultList = response
				.getAgencySubDistributorCommisionRates();
		
		if (config == null) {

			AgencySubDistributorCommisionRateVo expectResult = expectResultList
					.get(0);

			Logger.verifyEquals(expectResult.getDistributorLevelIndex(), 0,
					"验证Distributor_level_index");
			Logger.verifyEquals(expectResult.getDistributorLevelLabel(),
					"普通分销员", "验证Distributor_level_label");
			DataUtil.verifyDecimalEquals(expectResult.getLayer1Rate(), null,
					"验证Layer1_rate");
			
			return;
		}

		DistributionAgencySubDistributorCommisionRateWrapper distributionAgencySubDistributorCommisionRateWrapper = new DistributionAgencySubDistributorCommisionRateWrapper();
		
		List<DistributionAgencySubDistributorCommisionRate> actualResultList = distributionAgencySubDistributorCommisionRateWrapper
				.selectByConfigId(config.getId());

		for (int i = 0; i < actualResultList.size(); i++) {

			AgencySubDistributorCommisionRateVo expectResult = expectResultList
					.get(i);
			DistributionAgencySubDistributorCommisionRate actualResult = actualResultList
					.get(i);

			Logger.verifyEquals(getAgencyDistributorConfigReq.getMerchantId(),
					actualResult.getMerchant_id(), "验证Merchant_id");

			Logger.verifyEquals(expectResult.getDistributorLevelIndex(),
					actualResult.getDistributor_level_index(),
					"验证Distributor_level_index");
			Logger.verifyEquals(expectResult.getDistributorLevelLabel(),
					actualResult.getDistributor_level_label(),
					"验证Distributor_level_label");
			DataUtil.verifyDecimalEquals(expectResult.getLayer1Rate(),
					actualResult.getLayer1_rate(), "验证Layer1_rate");
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetAgencyDistributorConfig_001() {
		Logger.start(true, "商户的分销员机构的分销员配置，获取成功");
		
		try {
			
			getAgencyDistributorConfigReq.setMerchantId(500861041L);
			getAgencyDistributorConfigReq.setDistributorId(94932841L);
			
			getAgencyDistributorConfigCall.setData(getAgencyDistributorConfigReq);
			getAgencyDistributorConfigCall.callService();
			
			Logger.verifyEquals(true, getAgencyDistributorConfigCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetAgencyDistributorConfig_002() {
		Logger.start(true, "分销员配置未设置，结果为空");
		
		try {
			
			getAgencyDistributorConfigReq.setMerchantId(500861041L);
			getAgencyDistributorConfigReq.setDistributorId(1L);
			
			getAgencyDistributorConfigCall.setData(getAgencyDistributorConfigReq);
			getAgencyDistributorConfigCall.callService();
			
			Logger.verifyEquals(true, getAgencyDistributorConfigCall.getSuccess(), "验证success");
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetAgencyDistributorConfig_003() {
		Logger.start(true, "商户的CPS的分销员配置，获取成功");
		
		try {
			
			getAgencyDistributorConfigReq.setMerchantId(500861041L);
			getAgencyDistributorConfigReq.setDistributorId(656727L);
			
			getAgencyDistributorConfigCall.setData(getAgencyDistributorConfigReq);
			getAgencyDistributorConfigCall.callService();
			
			Logger.verifyEquals(true, getAgencyDistributorConfigCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
